package com.example.musicplayer;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.ObjectAnimator;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private static SeekBar seekBar;
    private static TextView tv_progress, tv_totaltime;
    private MusicService.MusicControl musicControl;
    private Intent intent;
    private MyServiceConn myConn;
    private ObjectAnimator animator;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        if(myConn != null){
            unbindService(myConn);
        }
    }
    private void init(){
        tv_progress = (TextView) findViewById(R.id.tv_time);
        tv_totaltime = (TextView) findViewById(R.id.tv_totaltime);
        seekBar = (SeekBar) findViewById(R.id.seekbar);
        findViewById(R.id.btn_play).setOnClickListener(this);
        findViewById(R.id.btn_Con).setOnClickListener(this);
        findViewById(R.id.btn_pause).setOnClickListener(this);
        findViewById(R.id.btn_stop).setOnClickListener(this);
        intent = new Intent(this,MusicService.class);
        myConn = new MyServiceConn();
        bindService(intent, myConn, BIND_AUTO_CREATE);
        ImageView iv_music = (ImageView) findViewById(R.id.iv_record);
        animator = ObjectAnimator.ofFloat(iv_music,"rotation",0f,360.0f);
        animator.setDuration(10000);
        animator.setInterpolator(new LinearInterpolator());
        animator.setRepeatCount(-1);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //播放
            case R.id.btn_play:
                musicControl.play();
                animator.start();
                break;
            //暂停
            case R.id.btn_pause:
                musicControl.pause();
                animator.pause();
                break;
            //继续
            case R.id.btn_Con:
                musicControl.continuePlayer();
                animator.start();
                break;
            //退出
            case R.id.btn_stop:
                if(myConn != null){
                    unbindService(myConn);
                    myConn = null;
                }
                finish();
                break;
            default:
                break;
        }
    }

    public static Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            //接收消息设置到进度条
            Bundle bundle = msg.getData();
            int duration = bundle.getInt("duration");
            int currentPos = bundle.getInt("currentPos");
            seekBar.setMax(duration);
            seekBar.setProgress(currentPos);
            int minute = duration/1000/60;
            int second = duration/1000%60;

            String strMin = null;
            String strSec = null;
            if(minute < 10)
                strMin = "0" + minute;
            else
                strSec = minute + "0";
            if(second < 10)
                strSec = "0" + second;
            else
                strSec = second + "";
            tv_totaltime.setText(strMin+":"+strSec);

            minute = currentPos/1000/60;
            second = currentPos/1000%60;
            if(minute < 10)
                strMin = "0" + minute;
            else
                strSec = minute + "0";
            if(second < 10)
                strSec = "0" + second;
            else
                strSec = second+"";
            tv_progress.setText(strMin+":"+strSec);
        }
    };
    class MyServiceConn implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            musicControl = (MusicService.MusicControl) iBinder;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    }
}